package com.example.antafter.utils;

import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Calendar;

/**
 * @authoer:WangMengqiang
 * @createDate:2022/9/15
 * @description:
 */
public class JiNianRiUtil {

    public static int getLianAi(){
        return calculateDays(LocalDate.parse("2019-04-23"), LocalDate.now());
    }

    /**
     * 计算农历生日
     * @param month
     * @param day
     * @param isLeapMonth
     * @return
     * @throws Exception
     */
    public static int getLunarBirthdayDays(int month, int day, boolean isLeapMonth) throws Exception{
        String birthdayDate = "";
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int[] dateArray = LunarCalendar.lunarToSolar(currentYear, month, day, isLeapMonth);
        int year = dateArray[0];
        String month2 = dateArray[1] < 10 ? "0" + dateArray[1] : String.valueOf(dateArray[1]);
        String day2 = dateArray[2] < 10 ? "0" + dateArray[2] : String.valueOf(dateArray[2]);
        birthdayDate = year + "-" + month2 + "-" + day2;
        int tempDays = calculateDays(LocalDate.now(), LocalDate.parse(birthdayDate));
        if (tempDays < 0){
            currentYear++;
            int[] nextDateArr = LunarCalendar.lunarToSolar(currentYear, month, day, isLeapMonth);
            year = nextDateArr[0];
            month2 = nextDateArr[1] < 10 ? "0" + nextDateArr[1] : String.valueOf(nextDateArr[1]);
            day2 = nextDateArr[2] < 10 ? "0" + nextDateArr[2] : String.valueOf(nextDateArr[2]);
            birthdayDate = year + "-" + month2 + "-" + day2;
        }
        return calculateDays(LocalDate.now(), LocalDate.parse(birthdayDate));
    }

    /**
     * 计算公历生日
     * @param month
     * @param day
     * @return
     * @throws ParseException
     */
    public static int calculateSolarBirthday(int month, int day) {
        String birthdayDate = "";
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        String month2 = month < 10 ? "0" + month : String.valueOf(month);
        String day2 = day < 10 ? "0" + day : String.valueOf(day);
        birthdayDate = currentYear + "-" + month2 + "-" + day2;
        LocalDate nowDate = LocalDate.now();
        int tempDays = calculateDays(nowDate, LocalDate.parse(birthdayDate));
        if (tempDays < 0){
            birthdayDate = ++currentYear + "-" + month2 + "-" + day2;
        }
        return calculateDays(nowDate, LocalDate.parse(birthdayDate));
    }

    //计算开始日期距结束日期多少天
    public static Integer calculateDays(LocalDate startDate, LocalDate endDate){
        try{
            return Math.toIntExact(startDate.until(endDate, ChronoUnit.DAYS));
        }catch (Exception e){
            System.out.println("日期计算异常");
            return 9999;
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getLianAi());
    }

}
